//
//  ImagePickerTool.m
//  DongDongCircle
//
//  Created by 谢曦 on 2016/12/22.
//  Copyright © 2016年 gl. All rights reserved.
//

#import "ImagePickerTool.h"
#import<AVFoundation/AVCaptureDevice.h>
#import<AVFoundation/AVMediaFormat.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import "UIImage+Extension.h"
#import "UIViewController+extension.h"
@interface ImagePickerTool ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    
}
@end
@implementation ImagePickerTool
UIImagePickerController *_pickerController;

+ (ImagePickerTool *)sharedManager
{
    static ImagePickerTool *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
    });
    return sharedAccountManagerInstance;
}

+(void)initialize
{

 _pickerController=[[UIImagePickerController alloc] init];
    _pickerController.delegate=[ImagePickerTool sharedManager];
    
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
//判断是否有访问权限
-(BOOL)sourceTypeAvailable:(UIImagePickerControllerSourceType)sorceType  {
   
    if (sorceType==UIImagePickerControllerSourceTypePhotoLibrary) {
           ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
      return   [[ImagePickerTool sharedManager] sourceTypeAvailable:sorceType authStatus:author];

        
    }
    if(sorceType==UIImagePickerControllerSourceTypeCamera){
        NSString *mediaType = AVMediaTypeVideo;
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
      return   [[ImagePickerTool sharedManager] sourceTypeAvailable:sorceType authStatus:authStatus];

    }
    else return NO;
    
   }

-(BOOL)sourceTypeAvailable:(UIImagePickerControllerSourceType)sorceType authStatus:(ALAuthorizationStatus)author
{

    NSString *alertStr=sorceType==UIImagePickerControllerSourceTypePhotoLibrary?[NSString stringWithFormat:@"当前未开启相册访问权限"]:[NSString stringWithFormat:@"当前未开启相机访问权限"];
    NSString *messageStr=UIImagePickerControllerSourceTypePhotoLibrary?[NSString stringWithFormat:@"请前往“设置”>“隐私”开启相册访问权限"]:[NSString stringWithFormat:@"请前往“设置”>“隐私”开启相机访问权限"];
   
    if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied){
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:alertStr message:messageStr preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionCancel=[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:actionCancel];
        
        [[UIViewController getCurrentVC] presentViewController:alert animated:YES completion:nil];
        return NO;
        
    }
    else return YES;



}
#pragma clang diagnostic pop

+(UIImagePickerController *)imagePickerController
{
    _pickerController.allowsEditing = YES;
    _pickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;

    
    return _pickerController;

}
+(UIImagePickerController *)cameraController
{
    _pickerController.sourceType=UIImagePickerControllerSourceTypeCamera;
    return _pickerController;
    
}
#pragma mark - ImagePickerControllerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    if ([self.delegate respondsToSelector:@selector(imagePickerControllerDidCancel)]) {
        [self.delegate imagePickerControllerDidCancel];
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//原始图片
    UIImage *upload=[image scaledToSize: CGSizeMake(1024, 1024)];
    if ([self.delegate respondsToSelector:@selector(imagePickerController:didPicker:userInfo:)]) {
        [self.delegate imagePickerController:picker didPicker:upload userInfo:info];
    }

}


-(UIImagePickerController *)pickerController
{
    return _pickerController;

}

@end
